using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using Sudoku;
using Sudoku.Lang;
using Sudoku.StateManagement;

namespace Sudoku.Menu
{
	public class Loader : ExtendedGameComponent
	{
		private enum LoadOrder
		{
			FONTs,
			SOUND_FXs,
			LOGOs,
			BACKGROUND_PART1,
			BACKGROUND_PART2,
			TEXTURE_PART1,
			TEXTURE_PART2,
			HOW_TOs
		}

		private LoadOrder state;
		private int done;

		private Texture2D bg;
		private Rectangle bgPos;

		private Texture2D bar;
		private Vector2 barPos;
		private Rectangle barSrc;

		private const int side = 256;

		public Loader(Game game)
			: base(game)
		{

		}

		public override void Initialize()
		{
			//we want a loop between update and draw
			Game.IsFixedTimeStep = false;

			state = LoadOrder.FONTs;
			done = 0;
			bgPos = new Rectangle( (int)(240 - ( side*0.5f )), (int)(400 - ( side*0.5f )), side, side);
			barPos = new Vector2(bgPos.X, bgPos.Y + bgPos.Height);
			barSrc = new Rectangle(0, 0, side, 0);

			base.Initialize();
		}

		protected override void Dispose(bool disposing)
		{
			Game.IsFixedTimeStep = true;
			base.Dispose(disposing);
		}

		protected override void LoadContent()
		{
#if SUDOKU_MASTER
			bar = Game.Content.Load<Texture2D>("Graphics/Texture/loaderM");
			bg = Game.Content.Load<Texture2D>("Graphics/Texture/loaderMBW");
#else
			bar = Game.Content.Load<Texture2D>("Graphics/Texture/loader");
			bg = Game.Content.Load<Texture2D>("Graphics/Texture/loaderBW");
#endif
			base.LoadContent();
		}

		public override void Update(GameTime gameTime)
		{
			switch (state)
			{
				case LoadOrder.FONTs:
					fontService.LoadContent();
					done += 15;
					break;

				case LoadOrder.SOUND_FXs:
					soundService.LoadSoundFx();
					done += 15;
					break;

				case LoadOrder.LOGOs:
					textureService.LoadLogos();
					done += 10;
					break;

				case LoadOrder.BACKGROUND_PART1:
					textureService.LoadBGs(1);
					done += 12;
					break;

				case LoadOrder.BACKGROUND_PART2:
					textureService.LoadBGs(2);
					done += 13;
					break;

				case LoadOrder.TEXTURE_PART1:
					textureService.LoadTextures(1);
					done += 12;
					break;

				case LoadOrder.TEXTURE_PART2:
					textureService.LoadTextures(2);
					done += 13;
					break;

				case LoadOrder.HOW_TOs:
					textureService.LoadHowToGraphics(LocalizedStrings.Language);
					done += 10;
					break;

				default:
					StateManager.SelectIntroScreen(Game);
					break;
			}

			state++;
			barSrc.Y = bg.Height - (int)(bg.Height * done * 0.01);
			barSrc.Height = (int)(bg.Height * done * 0.01);
			barPos.Y = barSrc.Y + bgPos.Y;

			base.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{

			spriteBatch.Begin();
			spriteBatch.Draw(bg, bgPos, Color.White);
			spriteBatch.Draw(bar, barPos, barSrc, Color.White);
			spriteBatch.End();

			base.Draw(gameTime);
		}
	}
}

